package com.bsj.sipgateway.web.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.constant.AlarmEnum;
import com.bsj.power.common.def.dto.AlarmQueryDTO;
import com.bsj.power.common.def.entity.alarmReport.AlarmReport;
import com.bsj.power.common.def.entity.log.VideoOpLog;
import com.bsj.power.common.def.vo.pls.AbnormalOfflineAlarmVO;
import com.bsj.power.common.def.vo.video.log.VideoOpLogExcelVO;
import com.bsj.power.common.def.vo.video.log.VideoOpLogExcelVO2;
import com.bsj.power.common.def.vo.video.log.VideoOpLogListVO;
import com.bsj.power.common.def.vo.video.log.VideoOpLogSearchVO;
import com.bsj.power.common.util.DateUtils;
import com.bsj.sipgateway.web.service.IVideoOpLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : zhuojie.c
 * @version V1.0
 * @Description: TODO
 * @date Date : 2024年08月13日
 * @menu 视频日志模块
 */
@Slf4j
@RestController
@RequestMapping("/videoOpLog")
//@SaCheckPermission("/video/videoOpLog")
public class VideoOpLogController {

    @Autowired
    @Qualifier("videoOpLogServiceImpl")
    private IVideoOpLogService videoOpLogService;

    /**
     * 视频日志列表
     *
     * @param videoOpLogSearchVO
     * @return
     */
    @GetMapping("/videoOpLogList")
    public JsonResult<List<VideoOpLogListVO>> videoOpLogList(@Validated VideoOpLogSearchVO videoOpLogSearchVO) {
        return videoOpLogService.videoOpLogList(videoOpLogSearchVO);
    }


    /**
     * 视频日志列表导出
     *
     * @param response
     * @param videoOpLogSearchVO
     * @throws IOException
     */
    @SaIgnore
    @GetMapping("/exportVideoOpLogList")
    public void exportVideoOpLogList(HttpServletResponse response, @Validated VideoOpLogSearchVO videoOpLogSearchVO) throws IOException {

        List<VideoOpLogExcelVO> videoOpLogExcelVOS = videoOpLogService.exportVideoOpLogExcelVO(videoOpLogSearchVO);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //编码 防止中文乱码
        String fileName = URLEncoder.encode("视频日志" + DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT2, new Date()), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcelFactory.write(response.getOutputStream(), VideoOpLogExcelVO.class).sheet("视频日志")
                .doWrite(videoOpLogExcelVOS);
    }

    /**
     * 分页查询设备异常行为报警
     *
     * @param alarmQueryDTO
     * @return
     */
    @SaIgnore
    @PostMapping("/abnormalOfflineAlarmList")
    public JsonResult<List<VideoOpLogListVO>> abnormalOfflineAlarmList(@RequestBody AlarmQueryDTO alarmQueryDTO) {
        return videoOpLogService.abnormalOfflineAlarmList(alarmQueryDTO);
    }

    /**
     * 分页查询设备异常离线报警
     *
     * @param alarmQueryDTO
     * @return
     */
    @SaIgnore
    @GetMapping("/exportAbnormalOfflineAlarmList")
    public void exportAbnormalOfflineAlarmList(HttpServletResponse response, AlarmQueryDTO alarmQueryDTO) throws IOException {
        List<VideoOpLogListVO> videoOpLogListVOS = videoOpLogService.exportAbnormalOfflineAlarmList(alarmQueryDTO);
        List<VideoOpLogExcelVO2> excelVOS = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(videoOpLogListVOS)) {
            for (int i = 0; i < videoOpLogListVOS.size(); i++) {
                VideoOpLogListVO videoOpLog = videoOpLogListVOS.get(i);
                VideoOpLogExcelVO2 excelVO = new VideoOpLogExcelVO2();
                excelVO.setSerialNum(i + 1);
                excelVO.setDeviceId(videoOpLog.getDeviceId());
                excelVO.setNickName(videoOpLog.getNickName());
//                excelVO.setOpUserName(videoOpLog.getOpUserName());
                excelVO.setName(videoOpLog.getName());
                excelVO.setContent(videoOpLog.getContent());
                excelVO.setOpTimeStr(DateUtils.dateToStr(videoOpLog.getOpTime()));
                excelVOS.add(excelVO);
            }
        }

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //编码 防止中文乱码
        String fileName = URLEncoder.encode("设备异常行为报警信息" + DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT2, new Date()), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcelFactory.write(response.getOutputStream(), VideoOpLogExcelVO2.class).sheet("设备异常行为报警信息")
                .doWrite(excelVOS);


    }


}
